<template>
  <div class="plan-detail-container">
    <div class="page-header">
      <h2>战术方案详情</h2>
      <div class="header-actions">
        <el-button type="primary" @click="exportPlan">导出方案</el-button>
        <el-button @click="$router.push('/tactical-plan/list')">返回列表</el-button>
      </div>
    </div>

    <el-row :gutter="20" v-loading="loading">
      <!-- 左侧方案信息 -->
      <el-col :span="16">
        <el-card shadow="hover" class="plan-info-card">
          <template #header>
            <div class="card-header">
              <h3>{{ planDetail.name }}</h3>
              <div class="plan-tags">
                <el-tag type="success" v-if="planDetail.type">{{ planDetail.type }}</el-tag>
                <el-tag type="warning" v-if="planDetail.scenario">{{ planDetail.scenario }}</el-tag>
                <el-tag type="info" v-if="planDetail.busType">{{ planDetail.busType }}</el-tag>
              </div>
            </div>
          </template>
          
          <!-- 方案描述 -->
          <div class="info-section">
            <h4 class="section-title">方案描述</h4>
            <p class="section-content">{{ planDetail.description }}</p>
          </div>

          <!-- 攻击类型和技术 -->
          <div class="info-section">
            <h4 class="section-title">攻击类型</h4>
            <div class="section-content">
              <el-tag type="danger" effect="dark">
                {{ planDetail.attackType === 'external' ? '外部环境渗透攻击' : '内部环境植入攻击' }}
              </el-tag>
            </div>
          </div>

          <div class="info-section">
            <h4 class="section-title">攻击方法</h4>
            <div class="section-content tech-tags">
              <el-tag 
                v-for="(tech, index) in planDetail.attackTechniques" 
                :key="index"
                effect="plain"
                class="tech-tag"
              >
                {{ tech }}
              </el-tag>
            </div>
          </div>

          <!-- 攻击流程 -->
          <div class="info-section">
            <h4 class="section-title">攻击流程</h4>
            <div class="flow-chart">
              <el-steps :active="planDetail.attackSteps?.length" direction="vertical">
                <el-step 
                  v-for="(step, index) in planDetail.attackSteps" 
                  :key="index"
                  :title="step.name"
                  :description="step.description"
                />
              </el-steps>
            </div>
          </div>

          <!-- 预期效果 -->
          <div class="info-section">
            <h4 class="section-title">预期效果</h4>
            <p class="section-content">{{ planDetail.expectedOutcome }}</p>
          </div>

          <!-- 方案资源 -->
          <div class="info-section" v-if="planDetail.resources?.length">
            <h4 class="section-title">方案资源</h4>
            <el-table :data="planDetail.resources" style="width: 100%">
              <el-table-column prop="name" label="资源名称" />
              <el-table-column prop="type" label="资源类型" width="120" />
              <el-table-column prop="size" label="大小" width="120" />
              <el-table-column label="操作" width="120">
                <template #default="scope">
                  <el-button type="primary" link @click="downloadResource(scope.row)">下载</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>

      <!-- 右侧信息和操作 -->
      <el-col :span="8">
        <!-- 方案元数据 -->
        <el-card shadow="hover" class="meta-card">
          <template #header>
            <div class="card-header">
              <h4>方案信息</h4>
            </div>
          </template>
          <ul class="meta-list">
            <li>
              <span class="meta-label">创建者：</span>
              <span class="meta-value">{{ planDetail.creator }}</span>
            </li>
            <li>
              <span class="meta-label">创建时间：</span>
              <span class="meta-value">{{ planDetail.createTime }}</span>
            </li>
            <li>
              <span class="meta-label">更新时间：</span>
              <span class="meta-value">{{ planDetail.updateTime || '无' }}</span>
            </li>
            <li>
              <span class="meta-label">使用次数：</span>
              <span class="meta-value">{{ planDetail.usageCount || 0 }} 次</span>
            </li>
            <li>
              <span class="meta-label">适用场景：</span>
              <span class="meta-value">{{ planDetail.scenario }}</span>
            </li>
            <li>
              <span class="meta-label">难度级别：</span>
              <span class="meta-value">
                <el-rate v-model="planDetail.difficultyLevel" disabled text-color="#ff9900" />
              </span>
            </li>
          </ul>
        </el-card>

        <!-- 操作卡片 -->
        <el-card shadow="hover" class="actions-card">
          <template #header>
            <div class="card-header">
              <h4>操作</h4>
            </div>
          </template>
          <div class="action-buttons">
            <el-button type="primary" @click="editPlan" icon="Edit">编辑方案</el-button>
            <el-button type="success" @click="executePlan" icon="VideoPlay">执行方案</el-button>
            <el-button @click="clonePlan" icon="CopyDocument">克隆方案</el-button>
            <el-button type="danger" @click="deletePlan" icon="Delete">删除方案</el-button>
          </div>
        </el-card>

        <!-- 相关方案 -->
        <el-card shadow="hover" class="related-card">
          <template #header>
            <div class="card-header">
              <h4>相关方案</h4>
            </div>
          </template>
          <div class="related-plans" v-if="relatedPlans.length">
            <el-collapse>
              <el-collapse-item v-for="(plan, index) in relatedPlans" :key="index" :title="plan.name">
                <div class="related-plan-info">
                  <p>{{ plan.description.substring(0, 100) }}...</p>
                  <el-button type="primary" link @click="viewRelatedPlan(plan)">查看详情</el-button>
                </div>
              </el-collapse-item>
            </el-collapse>
          </div>
          <el-empty v-else description="暂无相关方案" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
/**
 * 战术方案详情组件
 * @file detail.vue
 */

const route = useRoute()
const router = useRouter()
const planId = route.params.id

// 加载状态
const loading = ref(true)

// 方案详情数据
const planDetail = ref({})

// 相关方案
const relatedPlans = ref([])

// 获取方案详情
const fetchPlanDetail = () => {
  loading.value = true
  // 模拟API请求获取数据
  setTimeout(() => {
    planDetail.value = {
      id: planId,
      name: '工业控制系统CAN总线监听方案',
      type: '单兵作战',
      scenario: '工业自动化',
      busType: 'CAN',
      description: '本方案针对工业控制系统中的CAN总线通信进行监听，获取总线上传输的数据，用于后续的数据分析和漏洞挖掘。适用于初步了解目标系统的通信特性和控制指令，为进一步的渗透测试提供信息支持。',
      creator: 'admin',
      createTime: '2024-04-01 10:20:30',
      updateTime: '2024-04-02 15:32:46',
      usageCount: 12,
      difficultyLevel: 3,
      attackType: 'external',
      attackTechniques: ['链路嗅探', '数据重放'],
      attackSteps: [
        { name: '环境准备', description: '准备CAN总线分析仪、接口转换器等硬件设备' },
        { name: '接入总线', description: '按照指定方式接入CAN总线，不影响正常通信' },
        { name: '数据监听', description: '启动监听软件，配置正确的波特率和过滤条件' },
        { name: '数据分析', description: '对捕获的数据进行分析，识别关键帧和指令' }
      ],
      expectedOutcome: '成功获取CAN总线上的通信数据，能够解析出设备间通信的关键信息和控制指令，为进一步的攻击操作提供依据。',
      resources: [
        { name: 'CAN总线监听工具', type: '软件', size: '2.5MB' },
        { name: '监听分析指南', type: '文档', size: '1.2MB' },
        { name: '工控CAN总线通信协议说明', type: '文档', size: '3.8MB' }
      ]
    }

    // 模拟获取相关方案
    relatedPlans.value = [
      {
        id: 101,
        name: 'CAN总线消息劫持方案',
        description: '在CAN总线监听的基础上，实现对特定消息的劫持和替换，修改控制指令达到目标系统的异常行为。本方案包含完整的消息分析、劫持点确定和数据替换流程。'
      },
      {
        id: 102,
        name: 'CAN总线DoS攻击方案',
        description: '针对工业控制系统中的CAN总线通信，实施拒绝服务攻击，使总线处于高负载状态或阻断特定设备的通信，达到干扰系统正常运行的目的。'
      }
    ]

    loading.value = false
  }, 500)
}

// 在组件挂载时获取数据
onMounted(fetchPlanDetail)

// 导出方案
const exportPlan = () => {
  ElMessage.success('方案导出成功')
}

// 编辑方案
const editPlan = () => {
  router.push(`/tactical-plan/create?id=${planDetail.value.id}`)
}

// 执行方案
const executePlan = () => {
  ElMessage.success('方案已开始执行')
}

// 克隆方案
const clonePlan = () => {
  ElMessage.success('方案克隆成功')
}

// 删除方案
const deletePlan = () => {
  ElMessageBox.confirm(
    `确定要删除方案"${planDetail.value.name}"吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      router.push('/tactical-plan/list')
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除',
      })
    })
}

// 下载资源
const downloadResource = (resource) => {
  ElMessage.success(`正在下载: ${resource.name}`)
}

// 查看相关方案
const viewRelatedPlan = (plan) => {
  router.push(`/tactical-plan/detail/${plan.id}`)
}
</script>

<style lang="scss" scoped>
.plan-detail-container {
  padding: 20px;

    h2 {
      margin: 0;
    }

    .header-actions {
      display: flex;
      gap: 10px;
    }
  

  .plan-info-card {
    margin-bottom: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      h3 {
        margin: 0;
      }

      .plan-tags {
        display: flex;
        gap: 10px;
      }
    }

    .info-section {
      margin-bottom: 20px;

      &:last-child {
        margin-bottom: 0;
      }

      .section-title {
        font-size: 16px;
        font-weight: 500;
        margin-bottom: 8px;
        color: #303133;
        position: relative;
        padding-left: 10px;

        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
          width: 3px;
          height: 16px;
          background-color: #409EFF;
          border-radius: 2px;
        }
      }

      .section-content {
        color: #606266;
        line-height: 1.5;
      }

      .tech-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
      }

      .flow-chart {
        padding: 10px 0;
      }
    }
  }

  .meta-card, .actions-card, .related-card {
    margin-bottom: 20px;

    .card-header {
      h4 {
        margin: 0;
        color: #303133;
      }
    }

    .meta-list {
      list-style: none;
      padding: 0;
      margin: 0;

      li {
        display: flex;
        margin-bottom: 12px;
        
        .meta-label {
          font-weight: 500;
          color: #606266;
          min-width: 80px;
        }

        .meta-value {
          color: #303133;
        }
      }
    }

    .action-buttons {
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .related-plan-info {
      p {
        margin-top: 0;
        margin-bottom: 10px;
        color: #606266;
      }
    }
  }
}
</style> 